#include <stdio.h>
#include <string.h>
#include "func1.h"
#include "func2.h"
#include "Rk_wifi.h"
#include "gm_wifi.h"

extern int http_post_main();
extern int websockets_main();
extern int mqtt_basic_main();

extern int bluez5_main();
extern int http_get_xiaobing_secret(char *secret);
extern int get_user_bind_code();
extern int mpi_ai_record();
extern void mpi_play(char *path);
extern int duiplus_main();
extern void duiplus_wakup_init();

void *bluez_wifi_thr_fn(void *arg){
        printf("bluez_wifi_thr_fn begin\n");
        bluez5_main();
        printf("bluez_wifi_thr_fn end\n");
}

int main(int argc,char **argv)
{
        printf("Hello World, argc = %d\n", argc);
        func1();
        func2();

        if (argc < 2) {
                
                printf("no spetial task");
                return -1;
        }

        if (strcmp(argv[1], "main") == 0) {
                printf("bluez paring and wifi connect...\n");
                 start_wifi();

                int ret = is_paring_success_flag();
                printf("is_paring_success_flag ret = %d\n", ret);
                if (!ret) {
                        printf("user bind already, connect wifi\n");
                        char ssid[64] = {0};
                        char pwd[64] = {0};
                        get_save_ssid_pwd(ssid, pwd);
                        printf("ssid = %s, pwd = %s\n", ssid, pwd);

                        int wifi_conn_ret = connect_wifi(ssid, pwd);   
                        printf("wifi_conn_ret = %d\n", wifi_conn_ret);
                } else {
                        printf("user bind not yet, begin bluetooth pair and user bind\n");
                        pthread_t ntid;
                        int err;
                        err = pthread_create(&ntid, NULL, bluez_wifi_thr_fn, NULL);
                        if (err != 0)
                                printf("can't create thread");

                        while(1) {
                                int user_bind_code = get_user_bind_code();
                                usleep(100 * 1000);
                                if (user_bind_code == 0) {
                                        printf("user_bind_code == 0, sucess, break\n");
                                        break;
                                }
                        }

                        int attemp = 3;
                        while (attemp) {
                                char secret[128] = {0};
                                int ret = http_get_xiaobing_secret(secret);
                                if (ret == 0) {
                                        printf("http_post_main, secret = %s\n", secret);
                                        break;
                                }
                                usleep(2000 * 1000);   
                        }
                }

                websockets_main("file");
        }


        if (strcmp(argv[1], "http") == 0) {
                printf("http post, bluetooth user bind and active\n");
                //http_post_main(argv[2]);

                char secret[128] = {0};
                http_get_xiaobing_secret(secret);
        }

        if (strcmp(argv[1], "web") == 0) {
                printf("wensockets, xiaobing request\n");
                duiplus_wakup_init();
                websockets_main(argv[2]);
        }
        
        if (strcmp(argv[1], "mqtt") == 0) {
                printf("mqtt test\n");
                mqtt_basic_main();;
        }

	if (strcmp(argv[1], "wifi") == 0) {
		printf("wifi test\n");
                start_wifi();

                int ret = is_paring_success_flag();
                printf("is_paring_success_flag ret = %d\n", ret);
                if (!ret) {
                        char ssid[64] = {0};
                        char pwd[64] = {0};
                        get_save_ssid_pwd(ssid, pwd);
                        printf("ssid = %s, pwd = %s\n", ssid, pwd);

                        int ret1 = connect_wifi(ssid, pwd);   
                }
	}

        if (strcmp(argv[1], "mpi_play") == 0) {
		printf("mpi_play\n");
		mpi_play(argv[2]);
	}

        if (strcmp(argv[1], "mpi_record") == 0) {
                printf("mpi_record\n");
                mpi_ai_record();
        }

        if (strcmp(argv[1], "duiplus") == 0) {
                printf("duiplus\n");
                duiplus_main();
        }

    return 0;
}
